package org.jeecg.modules.simulator.model.line.station.station;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.jsonot.ODProbabilityApiInfo;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.person.path.PersonPath;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-11-27 10:01
 **/
public class PersonCreater {

    @Getter
    @Setter
    //key是o点站点ID-D点站点ID，value是路径概率信息，同一个od可能会有多条路径
    Map<String, List<ODProbabilityApiInfo>> odProbabilityApiInfosMap = new HashMap<>();
    @Getter
    @Setter
    Map<String, PersonPath> route2PersonPathMap = new HashMap<>();


    //原始的路径概率有可能因为过滤的情况，在路径表中找不到对应的路径，所以需要过滤
    Map<String, List<ODProbabilityApiInfo>> odProbabilityApiInfosFilterMap = new HashMap<>();





    //过滤掉od概率信息中不在路径表中的信息
    public void filterODProbabilityApiInfo() {
        for (Map.Entry<String, List<ODProbabilityApiInfo>> entry : odProbabilityApiInfosMap.entrySet()) {
            List<ODProbabilityApiInfo> filterList = entry.getValue().stream().filter(odProbabilityApiInfo -> {
                String routeId = odProbabilityApiInfo.getRouteId();
                return route2PersonPathMap.containsKey(routeId);
            }).collect(Collectors.toList());
            if (!filterList.isEmpty()) {
                odProbabilityApiInfosFilterMap.put(entry.getKey(), filterList);
            }
        }

    }

    public List<Person> loadHistoryPersons(int size) {
        List<Person> addPersonList = new ArrayList<>();
        long odCount = odProbabilityApiInfosFilterMap.values().stream()
                .flatMap(List::stream) // 将 List<ODProbabilityApiInfo> 展平为流
                .count(); // 统计总数
        if(odCount==0){
            throw new ArithmeticException("odCount超过为零");
        }
        if (odCount > Integer.MAX_VALUE) {
            throw new ArithmeticException("总数超过了整数的最大限制");
        }
        int totalCount = (int) odCount;
        //路径概率机会数大于
        if (totalCount > size) {
            return getPeople(size);
        } else {
            int remainder = size % totalCount;
            int baseSize = totalCount / size;
            for (int i = 0; i < baseSize; i++) {
                for (Map.Entry<String, List<ODProbabilityApiInfo>> entry : odProbabilityApiInfosFilterMap.entrySet()) {
                    for (ODProbabilityApiInfo info : entry.getValue()) {
                        String routeId = info.getRouteId();


                        Person person = addPersonForRoute(routeId);
                        addPersonList.add(person);
                    }
                }
            }
            List<Person> remainderPeople = getPeople(remainder);
            addPersonList.addAll(remainderPeople);
        }

        return addPersonList;
    }

    private List<Person> getPeople(int size) {
        List<Person> addPersonList = new ArrayList<>();
        int count = 0;
        for (Map.Entry<String, List<ODProbabilityApiInfo>> entry : odProbabilityApiInfosFilterMap.entrySet()) {
            //根据概率值排序
            Collections.sort(entry.getValue());
            //当添加书人数超过要添加的人数时，返回
            if (count >= size) {
                return addPersonList;
            }
            String routeId = entry.getValue().get(0).getRouteId();
            Person person = addPersonForRoute(routeId);
            addPersonList.add(person);
            count++;
        }
        return addPersonList;
    }

    private Person addPersonForRoute(String routeId) {
        PersonPath personPath = route2PersonPathMap.get(routeId);
        Person person = new Person(personPath);
        person.setId(UUID.randomUUID().toString());
        person.setInStationState();
        return person;
    }


}
